package com.example.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.activity.GoodsDetailActivity;
import com.example.adapter.TuanListAdapter;
import com.example.consts.Consts;
import com.example.dianping.R;
import com.example.entity.Goods;
import com.example.entity.ResponseObject;
import com.example.utils.ToastMaker;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnItemClick;

public class FragmentTuan extends Fragment {
	@ViewInject(R.id.lv_tuan_goods)
	PullToRefreshListView lv_tuan_goods;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_tuan, null);
		ViewUtils.inject(this, view);

		lv_tuan_goods.setMode(Mode.BOTH);
		lv_tuan_goods.setScrollingWhileRefreshingEnabled(true);
		lv_tuan_goods.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				System.out.println(lv_tuan_goods.getScrollY());
				loadDatas(lv_tuan_goods.getScrollY() < 0);
			}
		});
		new Handler(new Handler.Callback() {

			@Override
			public boolean handleMessage(Message msg) {
				lv_tuan_goods.setRefreshing();
				return true;
			}
		}).sendEmptyMessageDelayed(0, 300);

		return view;
	}

	// TODO 网络访问拿到数据 Xutils 模块的使用
	private int page, size = 10, count;

	private List<Goods> datas;
	private TuanListAdapter tuanListAdapter;

	private void loadDatas(final boolean reflush) {
		if (reflush) {
			page = 1;
		} else {
			page++;
		}
		RequestParams requestParams = new RequestParams();
		requestParams.addQueryStringParameter("page", page + "");
		requestParams.addQueryStringParameter("size", size + "");

		new HttpUtils().send(HttpMethod.GET, Consts.Gooods_Data_URI,
				requestParams, new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						lv_tuan_goods.onRefreshComplete();
						ToastMaker.showShortToast(arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						lv_tuan_goods.onRefreshComplete();
						Gson gson = new Gson();
						ResponseObject<List<Goods>> object = gson.fromJson(
								arg0.result,
								new TypeToken<ResponseObject<List<Goods>>>() {
								}.getType());
						page = object.getPage();
						size = object.getSize();
						count = object.getCount();

						// List<Goods> listDatas = object.getDatas();
						// datas = listDatas;
						//
						// // System.out.println(listDatas.size() + "-----" +
						// page
						// // + "-----" + size + "-----" + count);
						// TuanListAdapter tuanListAdapter = new
						// TuanListAdapter(
						// listDatas);
						// lv_tuan_goods.setAdapter(tuanListAdapter);
						if (reflush) {
							datas = object.getDatas();
							tuanListAdapter = new TuanListAdapter(datas);
							lv_tuan_goods.setAdapter(tuanListAdapter);
						} else {
							datas.addAll(object.getDatas());
							tuanListAdapter.notifyDataSetChanged();
						}
						// 当总页数与当前页数一致，表示无数据刷新，更改PullToReflush Mode
						// 为只能上拉刷新页面
						if (count == page) {
							lv_tuan_goods.setMode(Mode.PULL_FROM_START);
						}
					}
				});

	}

	@OnItemClick(R.id.lv_tuan_goods)
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(getActivity(), GoodsDetailActivity.class);
		intent.putExtra("goods", datas.get(position-1));
		startActivity(intent);
	}
}
